package cn.edu.ujn.finaltest.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.Orders;
import cn.edu.ujn.finaltest.dao.Product;
import cn.edu.ujn.finaltest.dao.User;
import cn.edu.ujn.finaltest.service.IOrdersService;
import cn.edu.ujn.finaltest.service.IProductService;
import cn.edu.ujn.finaltest.service.IUserService;
import cn.edu.ujn.finaltest.util.Page;

@Controller
public class OrdersController<E> {
	@Autowired
	private IOrdersService ordersService;
	
	@Autowired
	private IProductService productService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "/orders/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, String number, Integer userid, Model model) {
		Page<Orders> findOrderList = ordersService.findOrderList(page, rows, number, userid);
		List<Orders> orders = findOrderList.getRows();
		Product product = new Product();
		List<Product> products = productService.findAll(product);
		for(Orders order : orders) {
			order.setProductList(ordersService.selectByPrimaryKey(order.getId()).getProductList());
		}
		model.addAttribute("page", findOrderList);
		model.addAttribute("products", products);
		model.addAttribute("number", number);
		model.addAttribute("userid", userid);
		
		return "orders";
	}
	
	@RequestMapping(value ="/orders/create.action")
	@ResponseBody
	public String ordersCreate(Orders orders, HttpSession session, String product1, String product2, String product3) {
		// 获取Session中的当前用户信息
	    User user = (User) session.getAttribute("USER_SESSION");
	    // 将当前用户id存储在客户对象中
	    orders.setUserid(user.getUser_id());
	    // 创建Date对象
	    Date date = new Date();
	    // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
	    Timestamp timeStamp = new Timestamp(date.getTime());
	    orders.setCreatetime(timeStamp);
	   
	    List<Product> products = new ArrayList<Product>();
	    if(!StringUtils.isNullOrEmpty(product1)) {
	    	Product product = new Product();
		    product.setName(product1);
		    List<Product> productlist = productService.findAll(product);
		    if (productlist != null && productlist.size() > 0) {
			    product = productlist.get(0);
	        }
		    products.add(product);
	    }
	    if(!StringUtils.isNullOrEmpty(product2)) {
	    	Product product = new Product();
		    product.setName(product2);
		    List<Product> productlist = productService.findAll(product);
		    if (productlist != null && productlist.size() > 0) {
			    product = productlist.get(0);
	        }
		    products.add(product);
	    }
	    if(!StringUtils.isNullOrEmpty(product3)) {
	    	Product product = new Product();
		    product.setName(product3);
		    List<Product> productlist = productService.findAll(product);
		    if (productlist != null && productlist.size() > 0) {
			    product = productlist.get(0);
	        }
		    products.add(product);
	    }
	    orders.setProductList(products);
	   
	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = ordersService.insert(orders);
	    
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	
	@RequestMapping("/orders/delete.action")
	@ResponseBody
	public String ordersDelete(Integer id) {
	    int rows = ordersService.deleteByPrimaryKey(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
}
